﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace CMS {

    public class Token {
        public int responseCode;
        public string ResponseMessage;
    }

    public class ResponseToken : Token {

        public ResponseToken() { }

        public ResponseToken(int code) {
            responseCode = code;
            ResponseMessage = HumanReadableMessage.GenerateMessage(code);
        }
    }

    public class SoftwareType : ResponseToken{
        public SoftType softType;
        public SoftwareType(){}
        public SoftwareType(int code) : base(code){}
        public SoftwareType(int code, SoftType st) : base(code){
            softType = st;
        }
    }

    public class listOfCompIDs : ResponseToken {
        public DataSet list;
        public listOfCompIDs() { }
        public listOfCompIDs(int code) : base(code) { }
        public listOfCompIDs(int code, DataSet ds) : base(code) {
            list = ds;
        }
    }

    public class listOfCRs : ResponseToken {
        public CRArray data;
        public listOfCRs() { }

        public listOfCRs(int answer, CRArray crs)
            : base(answer) {
            data = crs;
        }

        public listOfCRs(int answer) : base(answer) { }
    }

    public class listOfComputers : ResponseToken {
        public ComputerArray data;
        public listOfComputers() { }
        public listOfComputers(int answer, ComputerArray ca) : base(answer) {
            data = ca;
        }
        public listOfComputers(int answer) : base(answer) { }
    }

    public class listOfSoftwares : ResponseToken {
        public SoftwareArray data;
        public listOfSoftwares () {}
        public listOfSoftwares( int answer, SoftwareArray sa) : base(answer){
            data = sa;
        }
        public listOfSoftwares(int answer):base(answer){}
    }

    public class listOfPartialStates : ResponseToken {
        public PSArray data;
        public listOfPartialStates() { }
        public listOfPartialStates(int answer, PSArray psa)
            : base(answer) {
            data = psa;
        }
        public listOfPartialStates(int answer) : base(answer) { }
    }

    public class listOfPlainTexts : ResponseToken{
        public PTArray data;
        public listOfPlainTexts() { }
        public listOfPlainTexts(int answer, PTArray pta)
            : base(answer) {
            data = pta;
        }
        public listOfPlainTexts(int answer) : base(answer) { }
    }

    public class listOfOperatingSystems : ResponseToken {
        public OSArray data;
        public listOfOperatingSystems() { }
        public listOfOperatingSystems(int answer, OSArray osa)
            : base(answer) {
            data = osa;
        }
        public listOfOperatingSystems(int answer) : base(answer) { }
    }

    public class listOfLicenses : ResponseToken {
        public licenseArray data;
        public listOfLicenses() { }
        public listOfLicenses(int answer, licenseArray la)
            : base(answer) {
            data = la;
        }
        public listOfLicenses(int answer) : base(answer) { }
    }

    public class loginStatus : User {
        //public string answer;       //human readable system response 
        //public int status;          //predefined status
        //public User user;
        public ResponseToken token;
        public string sessionID;    //unique session ID for login
        

        public loginStatus(){
        }
        //on error
        public loginStatus(int status, AuthentificatedUser pretender) {
            this.sessionID = pretender.SessionID;
            this.serviceGR = pretender.serviceGR;
            this.username = pretender.username;
            this.cmsRoles = pretender.cmsRoles;
            token = new ResponseToken(status);
        }

        public loginStatus(int status, string user) {
            this.username = user;
            token = new ResponseToken(status);
        }

        public loginStatus( int status){
            token = new ResponseToken(status);
        }

        private string SessionID {
            set { sessionID = value; }
        }
    }

    public class listOfCRsWithItems : ResponseToken {
        public CRWithDependencies CRzdep;
        public listOfCRsWithItems() { 
            CRzdep = new CRWithDependencies(); 
        }
        public listOfCRsWithItems(int code) : base(code) { 
            CRzdep = new CRWithDependencies(); 
        }
        public listOfCRsWithItems(int code, CRWithDependencies cr)
            : base(code) {
            CRzdep = new CRWithDependencies(); 
            CRzdep = cr;
        }

    }

    public class listOfCRsWithApproves : ResponseToken {
        public List<CRWithApproves> CRappr;
        public listOfCRsWithApproves() {
            CRappr = new List<CRWithApproves>();
        }
        public listOfCRsWithApproves(int code) : base(code) {
            CRappr = new List<CRWithApproves>();
        }
        public listOfCRsWithApproves(int code, CRWithApproves cr)
            : base(code) {
            CRappr = new List<CRWithApproves>();
            CRappr.Add(cr);
        }
        public void addNew(CRWithApproves cr) {
            CRappr.Add(cr);
        }
    }
    /*
     * is sent in response to attempt to log out
     */
    public struct logoutStatus {
        public ResponseToken response;
        public string username;
    
        public logoutStatus(int st, string user) {
            response = new ResponseToken(st);
            username = user;
        }
    }

    /*
     * Generates Human readable system response
     */
    public static class HumanReadableMessage {
        private static string message;
        
        public static string GenerateMessage(int messageCode) {            
            switch (messageCode) {
                case 1: message = "SUCCESS: login success";
                    break;
                case 2: message = "ERROR: username and password do not match";
                    break;
                case 3: message = "ERROR: Requested user is already logged in";
                    break;                
                case 5: message = "APPLICATION: LDAP authentification is not implemented";
                    break;
                case 6: message = "APPLICATION: Unknown type of authentification, check service settings config";
                    break;
                case 7: message = "SUCCESS: successfully logout";
                    break;
                case 8: message = "ERROR: Can not log out user - wrong credentials provided";
                    break;
                case 9: message = "ERROR: Can not log out user - user is not logged in";
                    break;                
                case 11: message = "Invalid hostname provided";
                    break;                
                case 13: message = "DATABASE:Update fail";
                    break;
                case 14: message = "ERROR User not logged in";
                    break;
                case 15: message = "DATABASE: Query sussess";
                    break;
                case 16: message = "ERROR: not enough privileges for the operation";
                    break;
                case 17: message = "APPLICATION: unknown application error";
                    break;
                case 18: message = "DATABASE: retrieve failure";
                    break;
                case 19: message = "CMS: You can not edit evluated CR";
                    break;
                case 20: message = "DATABASE: insert failure";
                    break;
                case 21: message = "DATABASE: user already exists";
                    break;
                default: message = "Unknown Status";
                    break;

            }
            return message;
        }
    }
}
